<?php

// +----------------------------------------------------------------------
// | 作品信息模型
// +----------------------------------------------------------------------

namespace Home\Model;

use Common\Model\Model;
use Common\Model\RelationModel;

class WorksModel extends RelationModel {

    protected $index=array();

    //关联模型
    protected $_link = array(
        'User' => array(
            'mapping_type'  => self::BELONGS_TO,
            'class_name'    => 'User',
            'foreign_key'   => 'userid',
            'as_fields' => 'nickname,uavatar,openuid,uweibo,uqqzone,fansnum,ifrz',
        ),
        'Category' => array(
            'mapping_type'  => self::BELONGS_TO,
            'class_name'    => 'Category',
            'foreign_key'   => 'cateid',
            'as_fields' => 'catename',
        ),
        'WorksMaterials' => array(
            'mapping_type'  => self::HAS_MANY,
            'class_name'    => 'WorksMaterials',
            'foreign_key'   => 'workid',
            'mapping_order'     => 'id ASC'
        ),
        'WorksMakestep' => array(
            'mapping_type'  => self::HAS_MANY,
            'class_name'    => 'WorksMakestep',
            'foreign_key'   => 'workid',
            'mapping_order'     => 'id ASC'
        ),

    );

   //首页推荐模型
    public function indextjwork(){
        $data=$this->where(array('ifindex'=>true,'status'=>true))->field('title,image,workid')->order('sort DESC')->limit(3)->select();
        cache('indextjwork',$data);
        return $data;
    }

    //首页每日新秀
    public function worksxinxiu(){
        $stime=strtotime(date('Y-m-d 00:00:00'));
        $etime=strtotime(date('Y-m-d 23:59:59'));
        $where['status']= array('EQ',true);
  //      $where['create_time']=array(array('GT',$stime),array('LT',$etime),'and');

        $model=new Model();
        $data=$model->table(array(
            C('DB_PREFIX').'works'=>'W',
            C('DB_PREFIX').'user'=>'U',
        ))
            ->where('W.userid=U.userid and W.status=true')
            ->field('W.image,W.title,W.clicknum,W.msgnum,W.workid,W.copynum,U.nickname,U.openuid,U.uavatar')
            ->order('W.clicknum DESC')
            ->limit(12)
            ->select();

       // $data=$this->where($where)->field(array('title','workid','clicknum','image'))->order('clicknum DESC')->limit(12)->select();
        cache('indexxinxiu',$data);
        return $data;
    }

    //首页每类别显示一条
    public function cateonework(){
        $where['status']=array('EQ',true);
        $where['iftj']=array('EQ',true);
        $where['type']=array('EQ','1');
     //   $data=$this->relation(array('User','Category'))->field('note,sort,status,iftj,ifindex,type',true)->where($where)->group('cateid')->order('create_time DESC')->select();

        $model=new Model();
        $data=$model->table(array(
            C('DB_PREFIX').'works'=>'W',
            C('DB_PREFIX').'category'=>'C',
            C('DB_PREFIX').'user'=>'U',
        ))
            ->where('W.userid=U.userid and W.status=true and W.iftj=true and W.type=1 and W.cateid=C.cateid')
            ->field('W.image,W.title,W.clicknum,W.msgnum,W.workid,W.goodnum,W.badnum,W.create_time,W.content,C.catename,C.cateid,U.nickname,U.openuid,U.uavatar')
            ->group('W.cateid')
            ->order('C.sort DESC,W.clicknum DESC')
            ->limit(12)
            ->select();

        return $data;
    }

    //动态作品
    public function dongtaiworks($type){

        $where['status']=array('EQ',true);

        $count = $this->where($where)->count();
        $page = $this->page($count, 20);


        $data['list'] = $this->relation(array('User','Category'))->field('note,sort,status,iftj,ifindex,type',true)->where($where)->order(''.$type.' DESC')->select();
     //   $data['page']=$page->show();
        return $data;
    }


}
